//
//  TailViewController.m
//  陶瓷
//
//  Created by 郭天龙 on 15/7/6.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "TailViewController.h"
#import "TCNetworkTool.h"
#import "TailTableViewCell.h"
#import "TailDetailViewController.h"

#import "MJRefresh.h"

typedef NS_ENUM(NSInteger, RefreshType) {
    PullUpRefresh,
    PushDownRefresh,
};


@interface TailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak) IBOutlet UIButton *leftButton;
@property (nonatomic, weak) IBOutlet UIButton *rightButton;
@property (nonatomic, assign) BOOL isSellType;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) IBOutlet UITableView *tableview;

@property (nonatomic, assign) int pn;

@property (nonatomic, assign) BOOL allCityState;

@property (nonatomic, strong) UIButton *cover;

@end

@implementation TailViewController


#pragma mark - Action
- (IBAction)clickSellButton:(id)sender
{
    _isSellType = YES;
    //    [self.tableview.header beginRefreshing];
    [self loadTailInfoWithType:PushDownRefresh];
    [self.leftButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}

- (IBAction)clickBuyButton:(id)sender
{
    _isSellType = NO;
    //    [self.tableview.header beginRefreshing];
    [self loadTailInfoWithType:PushDownRefresh];
    [self.rightButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}

- (IBAction)sendMessage {
    if (IsLoginIn) {
        AddMessageController *tcIssue = [[AddMessageController alloc] init];
        tcIssue.questionArray = tailGoodsQuestionArray;
        tcIssue.title = @"尾货信息发布";
        [self.navigationController pushViewController:tcIssue animated:YES];
    } else {
        self.tabBarController.selectedIndex = 4;
    }
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TailTableViewCell"];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"TailTableViewCell" owner:nil options:nil].firstObject;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if ([self.dataSource count] < indexPath.row) {
            return cell;
        }
        [cell setModel:[self.dataSource objectAtIndex:indexPath.row]];
    }
    return cell;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.navigationItem setTitle:@""];
    TailDetailViewController *tdvc = [[TailDetailViewController alloc] init];
    tdvc.proid = [[self.dataSource objectAtIndex:indexPath.row] objectForKey:@"Id"];
    [self.navigationController pushViewController:tdvc animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.、
    
    self.pn = 1;
    self.allCityState = YES;
    self.isSellType = YES;
    self.navigationController.navigationBar.translucent = NO;
    __weak UITableView *tableView = self.tableview;
    tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pn = 1;
        [self loadTailInfoWithType:PushDownRefresh];
    }];
    tableView.header.autoChangeAlpha = YES;
    tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _pn += 1;
        [self loadTailInfoWithType:PullUpRefresh];
    }];
    
    
    self.tableview.tableFooterView = [UIView new];
    
    if ([self.tableview respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableview setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self.tableview respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableview setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    _isSellType = YES;
    [self.leftButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self loadTailInfoWithType:PushDownRefresh];
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    addBtn.frame = CGRectMake(0, 0, 30,30);
    [addBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addBtn];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationItem setTitle:@"尾货中心"];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationItem setTitle:@""];
    [self.cover removeFromSuperview];
    [SVProgressHUD dismiss];
}

- (UIButton *)cover {
    if (_cover == nil) {
        _cover = [[UIButton alloc] initWithFrame:self.view.bounds];
        _cover.backgroundColor = [UIColor clearColor];
    }
    return _cover;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadTailInfoWithType:(RefreshType)type
{
    [SVProgressHUD showWithStatus:@"正在获取尾货列表 请稍后.."];
    [self.view addSubview:self.cover];
    TCBaseTool *base = [TCBaseTool sharedBaseTool];
    NSString *cityid = base.currentCityId;
    NSString *typestr;
    if (_isSellType) {
        typestr = @"0";
    }
    else
    {
        typestr = @"1";
    }
    if (type == PushDownRefresh) {
        TCNetworkTool *net = [TCNetworkTool sharedNetTool];
        [net getTailListWithPn:[NSString stringWithFormat:@"%d",self.pn] andDataType:typestr andCity:cityid SuccessBlocks:^(NSArray *resultArray) {
            if ([self.dataSource count] > 0) {
                [self.dataSource removeAllObjects];
            }
            self.dataSource = [resultArray mutableCopy];
            [self.tableview.header endRefreshing];
            [self.tableview reloadData];
            [self.cover removeFromSuperview];
            [SVProgressHUD dismiss];
        } andFailureBlocks:^(NSString *failureMessage) {
            [self.tableview.header endRefreshing];
            [SVProgressHUD dismiss];
            [self.cover removeFromSuperview];
            
        }];
    }
    else
    {
        TCNetworkTool *net = [TCNetworkTool sharedNetTool];
        [net getTailListWithPn:[NSString stringWithFormat:@"%d",self.pn] andDataType:typestr andCity:cityid SuccessBlocks:^(NSArray *resultArray) {
            [SVProgressHUD dismiss];
            [self.dataSource addObjectsFromArray:resultArray];
            [self.tableview.footer endRefreshing];
            [self.tableview reloadData];
            [self.cover removeFromSuperview];
        } andFailureBlocks:^(NSString *failureMessage) {
            [self.tableview.footer endRefreshing];
            [SVProgressHUD dismiss];
            
        }];
    }
    
}


@end
